package com.jiang.sign.Dao;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/*
* 重新UserDetails方法
* */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private SignUser signUser;
    private List<String>  permissionList;

    //存储SpringSecurity所需要的权限信息的集合
    @JSONField(serialize = false)
    private List<GrantedAuthority> authorities;

    public LoginUser(SignUser user,List<String> permissions) {
        this.signUser = user;
        this.permissionList = permissions;
    }

    //获取权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorities!=null){
            return authorities;
        }
        //把permissionList中的String类型的权限信息封装成SimpleGrantedAuthority对象
        authorities = permissionList.stream().
                map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        return authorities;
    }

    @Override
    public String getPassword() {
        return signUser.getPassword();
    }

    @Override
    public String getUsername() {
        return signUser.getUsername();
    }
    //是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
   //是否被锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    //是否没有超时
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    //是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
